﻿using System;
using System.ServiceModel;
using System.ServiceModel.Configuration;
using System.ServiceModel.Description;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Nvigorate.Common;
using Nvigorate.Common.Reflection;
using Nvigorate.WCF;

namespace Nvigorate.WCF.Test
{
    /// <summary>
    /// Summary description for ProxyTests
    /// </summary>
    [TestClass]
    public class ProxyTests
    {
        private static ServiceHost _host;

        public ProxyTests()
        {
            
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        private static string _address = @"http://localhost/testservice";
        private static ServiceEndpoint _endPoint;

        [ClassInitialize]
        public static void InitializeService(TestContext testContext)
        {
            _host = new ServiceHost(typeof(TestService)); 
            _host.CloseTimeout = TimeSpan.FromMilliseconds(100);
            _endPoint = _host.AddServiceEndpoint(typeof(ITestService), new BasicHttpBinding(BasicHttpSecurityMode.None),
                                     _address);
            _host.Description.Behaviors.Add(new ServiceMetadataBehavior() { HttpGetEnabled = true, HttpGetUrl = new Uri(_address) });
            

            _host.Open();
        }

        [ClassCleanup]
        public static void ShutDownService()
        {
            _host.Close();
            (_host as IDisposable).Dispose();
        }

        [TestMethod]
        public void ServiceClient_CallMethodViaProxy()
        {
            using (var client = new ServiceClient<ITestService>(_address))
            {
                var returned = client.Proxy.ReturnString("test");
                Assert.AreEqual("test", returned);
            }
        }

        [TestMethod]
        public void ServiceClient_CallMethodViaLambda()
        {
            using (var client = new ServiceClient<ITestService>(_address).AllTimeouts(TimeSpan.FromSeconds(2)))
            {
                var returned = client.Call(c => c.ReturnString("test"));
                Assert.AreEqual("test", returned);
            }
        }

        [TestMethod]
        public void ServiceFactory_GetByContract_IsNotNull()
        {
            var client = ServiceClientFactory.GetClient<ITestService>();
            Assert.IsNotNull(client);
        }

        [TestMethod]
        public void ServiceClient_CreateFromUrl_IsNotNull()
        {
            var client = new ServiceClient<ITestService>(_address);
            Assert.IsNotNull(client);
        }
    }
    
    
}
